package com.hengxinguotong.zhihuichengjian.net;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2018/7/30 9:34
 */

public class LogInterceptor implements Interceptor
{
    private final Charset UTF8 = Charset.forName("UTF-8");

    @Override
    public Response intercept(Chain chain) throws IOException
    {
        Request request = chain.request();
        RequestBody requestBody = request.body();

        String body = null;
        if (requestBody != null)
        {
            if (requestBody.contentLength() > 1024 * 1024)
            {
                body = "big file";
            } else
            {
                Buffer buffer = new Buffer();
                requestBody.writeTo(buffer);
                Charset charset = UTF8;
                MediaType contentType = requestBody.contentType();
                if (contentType != null)
                {
                    charset = contentType.charset(UTF8);
                }
                body = buffer.readString(charset);
            }
        }
        System.out.println("******************************* 请求开始 *******************************");
        System.out.println("请求方式：        " + request.method());
        if (request.body() != null)
        {
            System.out.println("请求Content-Type：" + request.body().contentType().toString());
        }
        System.out.println("请求URL：         " + request.url());
        System.out.println("请求headers：     " + request.headers());
        System.out.println("请求body：        " + body);
        System.out.println("******************************* 请求结束 *******************************");

        long startNs = System.nanoTime();
        Response response = chain.proceed(request);
        long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);

        String rBody = null;
        ResponseBody responseBody = response.body();
        if (requestBody != null) {
            if (responseBody.contentLength() > 1024 * 1024) {
                rBody = "big file";
            } else {
                BufferedSource source = responseBody.source();
                source.request(Long.MAX_VALUE); // Buffer the entire body.
                Buffer buffer = source.buffer();
                Charset charset = UTF8;
                MediaType contentType = responseBody.contentType();
                if (contentType != null) {
                    charset = contentType.charset(UTF8);
                }
                rBody = buffer.clone().readString(charset);
            }
        }
        System.out.println("******************************* 响应开始 *******************************");
        System.out.println("响应code：   " + response.code());
        System.out.println("响应message：" + response.message());
        System.out.println("响应耗时：   " + tookMs + "ms");
        System.out.println("响应URL：    " + response.request().url());
        System.out.println("响应body：" + rBody);
        System.out.println("******************************* 响应结束 *******************************");
        return response;
    }
}
